package com.zking.frame.TJ;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import com.zking.frame.LYK.HomepageJframe;
import com.zking.frame.TJ.JDBC.GetUserid;
import com.zking.frame.TJ.JDBC.baseDao;

import java.awt.Font;
import java.awt.Frame;

public class login extends JFrame implements MouseListener{
	
	private static final long serialVersionUID=11613L;
	

	private JLabel lb_warn;//顶部警告
	private JPanel pl_lg_main;
	
	private JLabel lb_logImg;//淘票票log
	private JLabel lb_title;//淘票票文字
	
	private JLabel lb_loginImg;//左边图片
	
	private JPanel pl_login;//登录
	private JLabel lb_loginTip;//登录提示
	private JLabel lb_user;//用户名
	private JTextField tf_username;//用户名输入框
	private JLabel lb_password;//密码
	private JPasswordField pf_password;//密码输入框
	private JLabel lb_forgetPassword;//忘记密码文字
	
	private JButton bt_login;//登录按钮
	private JLabel lb_registerTip;//注册提醒文字
	private JLabel lb_register;//注册入口
	
	public login() {
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		pl_lg_main=new JPanel();
		pl_lg_main.setLayout(null);
		pl_lg_main.setBackground(Color.WHITE);
		c.add(pl_lg_main);
	
		//顶部警告
		
		lb_warn = new JLabel(new ImageIcon(login.class.getResource("warn.png")));
		lb_warn.setBounds(302, 62, 907, 36);
		pl_lg_main.add(lb_warn);
		
		//淘票票log
		lb_logImg = new JLabel(new ImageIcon(login.class.getResource("log.jpg")));
		lb_logImg.setBounds(254, 159, 75, 75);
		pl_lg_main.add(lb_logImg);
		
		//淘票票文字
		lb_title = new JLabel("淘票票");
		lb_title.setFont(new Font("宋体", Font.PLAIN, 40));
		lb_title.setBounds(342, 159, 125, 75);
		lb_title.setForeground(Color.RED);
		pl_lg_main.add(lb_title);
		
		//左边图片
		lb_loginImg = new JLabel(new ImageIcon(login.class.getResource("login.png")));
		lb_loginImg.setBounds(349, 260,400, 362);
		pl_lg_main.add(lb_loginImg);
		
		pl_login = new JPanel();
		pl_login.setLayout(null);
		pl_login.setBackground(Color.WHITE);
		pl_login.setBounds(830, 260, 448, 287);
		pl_lg_main.add(pl_login);
		
		//登录提示
		lb_loginTip = new JLabel("账号登录");
		lb_loginTip.setFont(new Font("宋体", Font.PLAIN, 15));
		lb_loginTip.setBounds(14, 13, 60, 31);
		lb_loginTip.setForeground(Color.BLACK);
		pl_login.add(lb_loginTip);
		
		//用户名
		lb_user = new JLabel("用户名");
		lb_user.setBounds(52, 50, 50, 40);
		pl_login.add(lb_user);
		
		//用户名输入框
		tf_username = new JTextField();
		tf_username.setBounds(105, 50, 286, 40);
		pl_login.add(tf_username);
		
		//密码
		lb_password = new JLabel("密码");
		lb_password.setBounds(52, 100, 50, 40);
		pl_login.add(lb_password);
		
		//密码输入框
		pf_password = new JPasswordField();
		pf_password.setBounds(105, 100, 286, 40);
		pf_password.setToolTipText("密码");
		pl_login.add(pf_password);
		
		
		//忘记密码文字
		lb_forgetPassword = new JLabel("忘记密码?");
		lb_forgetPassword.setBounds(323, 153, 68, 31);
		lb_forgetPassword.setForeground(Color.RED);
		lb_forgetPassword.addMouseListener(new MouseAdapter(){
			
			@Override
			public void mouseClicked(MouseEvent e){
				String sql="select * from user where username ='"+tf_username.getText()+"'";
				com.zking.bean.User user =baseDao.findUsername(sql);
				if(tf_username.getText().isEmpty()) {
					JOptionPane.showMessageDialog(new JPanel(), "用户名不能为空","警告",JOptionPane.WARNING_MESSAGE);
				}else if(user==null) {
					JOptionPane.showMessageDialog(new JPanel(), "用户不存在","警告",JOptionPane.WARNING_MESSAGE);
				}else {
					new retrievePasswordFrame(login.this,true);
				}
			}
			
			@Override
			public void mouseEntered(MouseEvent e){//鼠标进入  
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));  
			}
			
			@Override
			public  void mouseExited(MouseEvent e){//鼠标移除  
				setCursor(Cursor.getDefaultCursor());
			}  
			
		});
		pl_login.add(lb_forgetPassword);
		
		//登录按钮
		bt_login = new JButton(new ImageIcon(login.class.getResource("bt_login.png")));
		bt_login.setBounds(105, 186, 273, 37);
		bt_login.setBackground(new Color(230,48,53));
		bt_login.setVisible(true);
		bt_login.setFocusable(false);
		bt_login.setBorder(null);
		bt_login.addMouseListener(new MouseAdapter(){
			
			@Override
			public void mouseEntered(MouseEvent e){//鼠标进入  
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));  
			}  
			
			@Override
			public  void mouseExited(MouseEvent e){//鼠标移除  
				setCursor(Cursor.getDefaultCursor());
			}  
			 
			@Override
			public void mouseClicked(MouseEvent e){
				String sql="select * from user where username ='"+tf_username.getText()+"'";
				com.zking.bean.User user =baseDao.findUsername(sql);
	
				if(tf_username.getText().isEmpty()) {
					JOptionPane.showMessageDialog(new JPanel(), "用户名不能为空","警告",JOptionPane.WARNING_MESSAGE);
				}
				else if(user==null){
					JOptionPane.showMessageDialog(new JPanel(), "用户不存在","警告",JOptionPane.WARNING_MESSAGE);
				}else if(!user.getPassword().equals(pf_password.getText())){
					JOptionPane.showMessageDialog(new JPanel(), "密码错误","警告",JOptionPane.WARNING_MESSAGE);
				}else {
					GetUserid.setId(user.getUser_id());
					dispose();
					new HomepageJframe();
					JOptionPane.showMessageDialog(null, "登陆成功", "登陆成功", JOptionPane.INFORMATION_MESSAGE);
				}
				
			}
		});
		pl_login.add(bt_login);
		
		//注册提醒文字
		lb_registerTip = new JLabel("还没有账号?");
		lb_registerTip.setBounds(52, 230, 83, 31);
		pl_login.add(lb_registerTip);
		
		//注册入口
		lb_register = new JLabel("免费注册");
		lb_register.setBounds(136, 230, 83, 31);
		lb_register.setForeground(Color.RED);
		lb_register.addMouseListener(new MouseAdapter(){
			
			@Override
			public void mouseClicked(MouseEvent e){
				new register();
				dispose();
			}
			
			@Override
			public void mouseEntered(MouseEvent e){//鼠标进入  
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));  
			}
			
			@Override
			public  void mouseExited(MouseEvent e){//鼠标移除  
				setCursor(Cursor.getDefaultCursor());
			}
			
		});
		pl_login.add(lb_register);
		
		setTitle("淘票票网");//标题
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//点击关闭按钮退出
		setSize(1500, 1000);//尺寸
		setLocationRelativeTo(null);//居中
		setVisible(true);//可见
	}
	
	//创建子页面
	class retrievePasswordFrame extends JDialog{
		private static final long serialVersionUID = 585123L;
		public retrievePasswordFrame(Frame owner, boolean modal) {
			super(owner, modal);
			
			JPanel pl_smain = new JPanel();
			pl_smain=new JPanel();
			pl_smain.setLayout(null);
			getContentPane().add(pl_smain);
			
			JLabel lb_retrieveUsername = new JLabel("请输入密保手机",JLabel.CENTER);
			lb_retrieveUsername.setBounds(50, 50, 100, 35);
			pl_smain.add(lb_retrieveUsername);
			
			JTextField tf_retrieveUsername = new JTextField();
			tf_retrieveUsername.setBounds(165, 50, 150, 35);
			pl_smain.add(tf_retrieveUsername);
			
			JButton bt_retrievePassword = new JButton("找回密码");
			bt_retrievePassword.setBounds(75, 120, 200, 35);
			bt_retrievePassword.setFocusPainted(false);
			bt_retrievePassword.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					String sql="select * from user where username ='"+tf_username.getText()+"'";
					com.zking.bean.User user =baseDao.findUsername(sql);
					long a = Long.parseLong(tf_retrieveUsername.getText());
					long b = Long.parseLong(user.getPhone());
					if(a==b) {
						JOptionPane.showMessageDialog(null, "您的密码是："+user.getPassword(), "找回密码", JOptionPane.INFORMATION_MESSAGE);
						dispose();
					}else {
						JOptionPane.showMessageDialog(new JPanel(), "密保手机输入错误","警告",JOptionPane.WARNING_MESSAGE);
					}
				}
			});
			pl_smain.add(bt_retrievePassword);
			
			setTitle("找回密码");
			setSize(400, 300);
			setLocationRelativeTo(null);
			setVisible(true);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		}
		
	}
	
	public static void main(String[] args) {
		new login();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}
}
